//
//  HelpViewController.m
//  YYPHSpeedJump
//
//  Created by speedjump on 16/1/16.
//  Copyright © 2016年 sujiang. All rights reserved.
//

#import "HelpViewController.h"
#import "HelpTableViewCell.h"
#import "NSString+Size.h"
@interface HelpViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *titleArray;
@property (nonatomic, strong) NSMutableArray *contextArr;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(strong,nonatomic)NSMutableArray *sectionStates;


@end

@implementation HelpViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新手指南";
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = kColors(239, 239, 241, 1);
    self.tableView.backgroundColor = kColors(239, 239, 241, 1);

//    UIScrollView *scrollV = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    [self.view addSubview:scrollV];
//    scrollV.contentSize = CGSizeMake(0, kScreenHeight *3 + 110);
//    scrollV.bounces = NO;
//    scrollV.showsVerticalScrollIndicator = NO;
//    
//    UIImageView *imageView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight *3)];
//    [scrollV addSubview:imageView];
//    imageView.image = [UIImage imageNamed:@"ssss"];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIView *heafeView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    self.tableView.tableHeaderView = heafeView;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.sectionStates = [NSMutableArray array];
    for (NSInteger i = 0; i <6; i++) {
        [self.sectionStates addObject:@NO];
    }
}

- (NSMutableArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = [NSMutableArray arrayWithObjects: @"基础操作和简介",@"速降", @"心愿墙", @"晒单", @"个人",@"注意事项",nil];
    }
    return _titleArray;
}

- (NSMutableArray *)contextArr
{
    if (!_contextArr){
        UIImage *imageJC = [UIImage imageNamed:@"1首页"];
        UIImage *imageSJ = [UIImage imageNamed:@"2速降页"];
        UIImage *imageXYQ = [UIImage imageNamed:@"3心愿墙"];
        UIImage *imageSD = [UIImage imageNamed:@"4晒单"];
        UIImage *imageGR = [UIImage imageNamed:@"5个人"];
        UIImage *imageZYSX = [UIImage imageNamed:@"注意事项"];
        _contextArr = [NSMutableArray arrayWithObjects:imageJC, imageSJ, imageXYQ, imageSD, imageGR,imageZYSX, nil];
    }
    return _contextArr;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([_sectionStates[section] boolValue]==YES) {
        return 1;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArray.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(18, 44/2 - 44/2, 200, 44)];
    label.text = self.titleArray[section];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:17];
    label.textColor = kColors(51, 51, 51, 1);
    [view addSubview: label];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 20 - 26, 44/2, 15, 10)];
    imageView.image = [UIImage imageNamed:@"newUserGuide_jiaotou"];
    [view addSubview:imageView];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth - 100, 0, 100, 44)];
    //[button setImage:[UIImage imageNamed:@"newUserGuide_jiaotou"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(sectionHeaderButtonOnClick:) forControlEvents:UIControlEventTouchUpInside];
    button.tag = 100 + section;
    [view addSubview:button];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 43, kScreenWidth, 1)];
    lineView.backgroundColor = kColors(239, 239, 241, 1);
    [view addSubview:lineView];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIImage *image = self.contextArr[indexPath.section];
    
//    NSArray *ary = [str componentsSeparatedByString:@"\n"];
//    CGFloat h = 0;
//    for (NSString *comStr in ary) {
//        h = h + [comStr heightWithFont:[UIFont systemFontOfSize:14] constrainedToWidth:kScreenWidth - 20];
//    }
    CGFloat imageH = (kScreenWidth/image.size.width)*image.size.height;
    return imageH;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HelpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"helpCell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"HelpTableViewCell" owner:nil options:nil]lastObject];
        //cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    //cell.contextLb.text = self.contextArr[indexPath.section];
    cell.helpImageView.image = self.contextArr[indexPath.section];
    cell.contentView.backgroundColor = kColors(239, 239, 241, 1);
    //cell.contextLb.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"k"]];
   // cell.contextLb.contentMode = UIViewContentModeScaleToFill;
    
    return cell;
}
-(void)sectionHeaderButtonOnClick:(UIButton*)btn
{
    if ([_sectionStates[btn.tag-100] boolValue]==YES) {
        
        [_sectionStates removeObjectAtIndex:btn.tag-100];
        [_sectionStates insertObject:@NO atIndex:btn.tag-100];
    }
    else
    {
        btn.transform=CGAffineTransformMakeRotation(0);
        
        [_sectionStates removeObjectAtIndex:btn.tag-100];
        [_sectionStates insertObject:@YES atIndex:btn.tag-100];
    }
    NSIndexSet *indexSet=[NSIndexSet indexSetWithIndex:btn.tag-100];
    
    //[self.tableView reloadData];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];

}
//- (void)didReceiveMemoryWarning {
//    [super didReceiveMemoryWarning];
//    // Dispose of any resources that can be recreated.
//}
//
//- (CGFloat)cellHeightWithContext:(NSString *)context
//{
//    CGRect frame = [context boundingRectWithSize:CGSizeMake(kScreenWidth - 20, 100000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
//    return frame.size.height;
//}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
@end
